package org.example.nebula.common.config;

import com.vesoft.nebula.client.graph.NebulaPoolConfig;
import com.vesoft.nebula.client.graph.data.HostAddress;
import com.vesoft.nebula.client.graph.net.NebulaPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.UnknownHostException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: jszhang
 * @date: 2023/2/13-10:25
 */
@Configuration
@EnableConfigurationProperties({NebulaGraphProperties.class})
public class NebulaGraphConfig {

    @Autowired
    private NebulaGraphProperties nebulaGraphProperties;

    @Bean(destroyMethod = "close")
    public NebulaPool nebulaPool() throws UnknownHostException {
        final List<HostAddress> addressList = nebulaGraphProperties.getHostAddresses().stream().map(s -> {
            final String[] split = s.split(":");
            return new HostAddress(split[0], Integer.parseInt(split[1]));
        }).collect(Collectors.toList());

        NebulaPoolConfig nebulaPoolConfig = new NebulaPoolConfig();
        nebulaPoolConfig.setMaxConnSize(10);
        NebulaPool pool = new NebulaPool();
        pool.init(addressList, nebulaPoolConfig);
        return pool;
    }

}